package com.cardvalue.sys.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardvalue.sys.MyApplication;
import com.cardvalue.sys.R;
import com.cardvalue.sys.activity.ChangePwdActivity;
import com.cardvalue.sys.tool.NetworkUtil;
import com.cardvalue.sys.tool.ReceiverDialog;

/**
 * Created by cardvalue on 2016/3/30.
 */
public class BaseActivity extends Activity {
    public ProgressDialog dialog;
    public ReceiverDialog rd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog=new ProgressDialog(getApplicationContext());
        MyApplication.getInstance().pushActivity(this);
        rd=new ReceiverDialog();
    }

    /**
     * 更新UI
     */
    public void  updataUI(){//int updataU
        // 发送广播
        Intent intentupdataUI = new Intent();
        intentupdataUI.setAction("com.cardvalue.sys.activitys.BasicLimit");
        //intentupdataUI.putExtra("updataUI",updataUI);
        this.sendBroadcast(intentupdataUI);
    }


    /******************************************************************************************************
     * headFieldIds显示内空的资源id
     * 设置顶部文字显示，顺序为［文字左，文字中，文字右，图片左，图片中，图片右］，不显示可传入0
     * @param
     * ****************************************************************************************************/
    protected void setHeaderFields(int... headFieldIds) {
        final int fieldIds[] = { R.id.tv_left, R.id.tv_center, R.id.tv_right,
                R.id.iv_left, R.id.iv_center, R.id.iv_right };
        for (int i = 0; i < headFieldIds.length; i++) {
            if (headFieldIds[i] == 0) {
                continue;
            }
            if (i <= 2) {
                View view = findViewById(fieldIds[i]);
                if (view instanceof TextView) {
                    TextView tv = (TextView) view;
                    tv.setVisibility(View.VISIBLE);
                    tv.setText(getString(headFieldIds[i]));
                }
            } else {
                View view = findViewById(fieldIds[i]);
                if (view instanceof ImageView) {
                    ImageView iv = (ImageView) view;
                    iv.setVisibility(View.VISIBLE);
                    iv.setImageResource(headFieldIds[i]);
                }
            }
        }
    }
    /**
     * 点击空白处  键盘消失
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println("down");
            if (this.getCurrentFocus() != null) {
                if (this.getCurrentFocus().getWindowToken() != null) {
                    //调用系统自带的隐藏软键盘
                    ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
        return super.onTouchEvent(event);
    }


    //点击返回时的操作
    public void goBack(View view){
        this.finish();
    }
    /**
     * 判断当前是否有网络。
     */
    public boolean isNetWorkConnect(){
        return NetworkUtil.isNetworkConnected(this);
    }
}
